/*
 * Control all the worker qualifications
 */
HCM.WorkerQualifications = new (function(el) {
    var j_root_el;
    var default_worker_qualification;
    var worker_qualifications = new Array();
    var add_qualification_contailer;
    var add_qualification_btn;

    this.onReady = function(el) {
        j_root_el = $(el);
        add_qualification_contailer = j_root_el.find('.add_qualification_requirement');
        add_qualification_btn = add_qualification_contailer.find('a');
        default_worker_qualification = j_root_el.find('.qualification_requirement').first();
        j_root_el.find('.qualification_requirement').each(function(){
            worker_qualifications.push(new HCM.WorkerQualification(this));
        });

        add_qualification_btn.click(function(){
            add_qualification_contailer.before(default_worker_qualification.clone());
            return false;
        });
    };
});

/*
 * Just define one worker qualification
 */
HCM.WorkerQualification = function(el) {
    var j_root_el = $(el);
    var remove_btn;
    var qualification_type_select;

    this.onReady = function() {
        remove_btn = j_root_el.find('a.remove_qualification_requirement');
        qualification_type_select = j_root_el.find('select.qualification_type_id');
        
        remove_btn.click(function(){
            j_root_el.remove();
            return false;
        });

        qualification_type_select.change(function(){
            qualification_type_select.find("option:selected").each(function(){
                update(this);
            });
        });
    };

    var update = function(option) {
        var j_option = $(option);
        if (j_option.hasClass('integer')) {
            j_root_el.find('div').hide();
            j_root_el.find('.integerComparators').css('display', 'inline');
            j_root_el.find('.integerValues').css('display', 'inline');
        } else if (j_option.hasClass('locale')) {
            j_root_el.find('div').hide();
            j_root_el.find('.singleValueComparators').css('display', 'inline');
            j_root_el.find('.localeValues').css('display', 'inline');
        } else if (j_option.hasClass('required')) {
            j_root_el.find('div').hide();
            j_root_el.find('.qual_required').css('display', 'inline');
        } 
    };

    this.onReady();
};

$(function() {
    $('.workerQualification').each(function () {
        HCM.WorkerQualifications.onReady(this);
    });
});

